<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Withdrawal_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $process_type = 0;
	var $created_date = '';
	var $expiration_date = '';
	var $paid_date = 0;
	var $company = 0;
	var $comment = 0;
	var $status = 0;
	var $cost = 0;
	var $unit = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setProcessType($input){
		$this->process_type = $input;
	}
	function setCreatedDate($input){
		$this->created_date = $input;
	}
	function setExpirationDate($input){
		$this->expiration_date = $input;
	}
	function setPaidDate($input){
		$this->paid_date = $input;
	}
	function setCompany($input){
		$this->company = $input;
	}
	function setComment($input){
		$this->comment = $input;
	}
	function setStatus($input){
		$this->status = $input;
	}
	function setCost($input){
		$this->cost = $input;
	}
	function setUnit($input){
		$this->unit = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getProcessType($input){
		return $this->process_type;
	}
	function getCreatedDate(){
		return $this->created_date;
	}
	function getExpirationDate(){
		return $this->expiration_date;
	}
	function getPaidDate(){
		return $this->paid_date;
	}
	function getCompany(){
		return $this->company;
	}
	function getComment(){
		return $this->comment;
	}
	function getStatus(){
		return $this->status;
	}
	function getCost(){
		return $this->cost;
	}
	function getUnit(){
		return $this->unit;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_WITHDRAWAL)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->process_type = $result[0]->process_type;
			$this->created_date = $result[0]->created_date;
			$this->expiration_date = $result[0]->expiration_date;
			$this->paid_date = $result[0]->paid_date;
			$this->company = $result[0]->company;
			$this->comment = $result[0]->comment;
			$this->status = $result[0]->status;
			$this->cost = $result[0]->cost;
			$this->unit = $result[0]->unit;
			return $this;
		}
		return false;
	}
	
	function getList($limit,$base_where){
		$ci=& get_instance();
		if(isset($base_where['company'])){
			$ci->db->like('company', $base_where['company']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_date >= ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_date <= ', strtotime($base_where['to']));
		}
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_date', 'DESC');
		return $ci->db->get(TBL_WITHDRAWAL)->result();
	}
	function count($base_where){
		$ci=& get_instance();
		if(isset($base_where['company'])){
			$ci->db->like('company', $base_where['company']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_date > ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_date < ', strtotime($base_where['to']));
		}
		return $ci->db->count_all_results(TBL_WITHDRAWAL);
	}
	
	function save(){
		$ci=& get_instance();
		$this->process_type = $result[0]->process_type;
		$this->created_date = $result[0]->created_date;
		$this->expiration_date = $result[0]->expiration_date;
		$this->paid_date = $result[0]->paid_date;
		$this->company = $result[0]->company;
		$this->comment = $result[0]->comment;
		$this->status = $result[0]->status;
		$this->cost = $result[0]->cost;
		$this->unit = $result[0]->unit;
		$ci->db->insert(TBL_WITHDRAWAL);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('agent_id',$this->getCommercialId());
		$ci->db->set('agent_username',$this->getAgentUsername());
		$ci->db->set('agent_name',$this->getAgentName());
		$ci->db->set('rate_from',$this->getRateFrom());
		$ci->db->set('rate_to',$this->getRateTo());
		$ci->db->set('actual_registered',$this->getActualRegistered());
		$ci->db->set('active_from',$this->getActiveFrom());
		$ci->db->set('active_to',$this->getActiveTo());
		$ci->db->set('discount_amount_per_register',$this->getDiscountAmountPerRegister());
		$ci->db->set('discount_unit',$this->getDiscountUnit());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		return $ci->db->where('id', $this->id)->update(TBL_WITHDRAWAL);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_WITHDRAWAL);
	}
	public function countPendingWithdrawal(){
		$ci=& get_instance();
		$ci->db->where('status', 'no');
		$ci->db->where('paid_date < '.time());
		return $ci->db->count_all_results(TBL_WITHDRAWAL);
	}
}